package com.alan.dfs.msg;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author shixing
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@ApiModel("RestResponse")
public class RestResponse<T> {

    @ApiModelProperty(value = "error code", required = true)
    private long code;
    @ApiModelProperty(value = "error message", required = true)
    private String message;
    @ApiModelProperty(value = "data", required = true)
    private T data;

    public RestResponse data(T data) {
        return ok(data);
    }
    public static <T> RestResponse<T> ok() {
        return ok(Code.SUCCESS);
    }

    public static <T> RestResponse<T> ok(Code code) {
        return ok(code, null);
    }

    public static <T> RestResponse<T> ok(T data) {
        return ok(Code.SUCCESS, data);
    }

    public static <T> RestResponse<T> ok(Code code, T data) {
        return new RestResponse<>(code, data);
    }

    public static <T> RestResponse<T> error(Code code) {
        return ok(code);
    }

    public static <T> RestResponse<T> error(int errorCode, String message) {
        return new RestResponse<>(errorCode, message, null);
    }

    public RestResponse() {}

    public RestResponse(T data) {
        this(Code.SUCCESS, data);
    }

    public RestResponse(Code code) {
        this(code.getCode(), code.getMessage(), null);
    }

    public RestResponse(Code code, T data) {
        this(code.getCode(), code.getMessage(), data);
    }

    public RestResponse(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
}
